#include "qtbox2dtest.h"
#include <QActionGroup>

QtBox2DTest::QtBox2DTest(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags), mVerticalLayout(0), mVerticalLayoutWidget(0)
{
	ui.setupUi(this);

	ui.mainToolBar->addAction(ui.actionNew);
	ui.mainToolBar->addAction(ui.actionOpen);
	ui.mainToolBar->addAction(ui.actionSave);
	ui.mainToolBar->addAction(ui.actionSaveAs);
	ui.mainToolBar->addSeparator();
	ui.mainToolBar->addAction(ui.actionUndo);
	ui.mainToolBar->addAction(ui.actionRedo);
	ui.mainToolBar->addSeparator();
	ui.mainToolBar->addAction(ui.actionCopy);
	ui.mainToolBar->addAction(ui.actionCut);
	ui.mainToolBar->addAction(ui.actionPaste);
	ui.mainToolBar->addAction(ui.actionDelete);
	ui.mainToolBar->addSeparator();
	ui.mainToolBar->addAction(ui.actionTex);
	ui.mainToolBar->addSeparator();
	ui.mainToolBar->addAction(ui.actionRect);
	ui.mainToolBar->addAction(ui.actionCircle);

	//mPolygonAction = new QAction(this);
	//connect(ui.actionFive, SIGNAL(triggered()), mPolygonAction, SLOT(triggered()));
	//mPolygonAction->setIcon(ui.actionFive->icon());
	//mPolygonAction->setMenu(ui.menuPolygon);
	connect(ui.menuPolygon, SIGNAL(triggered(QAction*)), this, SLOT(polygonTriggered(QAction*)));
	//connect(ui.menuPolygon, SIGNAL(triggered(QAction*)), this, SIGNAL(createTriggered(QAction*)));
	ui.mainToolBar->addAction(ui.menuPolygon->menuAction());
	ui.menuPolygon->setDefaultAction(ui.actionFive);
	connect(ui.menuPolygon->menuAction(), SIGNAL(triggered()), ui.actionFive, SIGNAL(triggered()));
	
	ui.mainToolBar->addSeparator();
	ui.mainToolBar->addAction(ui.actionPlay);


	mVerticalLayoutWidget = new QWidget(ui.dockWidgetContents);

	connect(ui.actionNew, SIGNAL(triggered()), this, SIGNAL(newFile()));
	connect(ui.actionSave, SIGNAL(triggered()), this, SIGNAL(save()));
	connect(ui.actionOpen, SIGNAL(triggered()), this, SIGNAL(load()));
	connect(ui.actionSaveAs, SIGNAL(triggered()), this, SIGNAL(saveAs()));

	connect(ui.actionUndo, SIGNAL(triggered()), this, SIGNAL(undo()));
	connect(ui.actionRedo, SIGNAL(triggered()), this, SIGNAL(redo()));
	connect(this, SIGNAL(enableUndo(bool)), ui.actionUndo, SLOT(setEnabled(bool)));
	connect(this, SIGNAL(enableRedo(bool)), ui.actionRedo, SLOT(setEnabled(bool)));

	connect(ui.actionCopy, SIGNAL(triggered()), this, SIGNAL(copy()));
	connect(ui.actionCut, SIGNAL(triggered()), this, SIGNAL(cut()));
	connect(ui.actionPaste, SIGNAL(triggered()), this, SIGNAL(paste()));

	connect(ui.actionDelete, SIGNAL(triggered()), this, SIGNAL(deleteItem()));

	connect(ui.actionPlay, SIGNAL(triggered(bool)), this, SIGNAL(start(bool)));

	connect(ui.menuCreate, SIGNAL(triggered (QAction*)), this, SIGNAL(createTriggered (QAction*)));

	connect(ui.actionTex, SIGNAL(triggered()), this, SIGNAL(showImageDlg()));
}

QtBox2DTest::~QtBox2DTest()
{

}

void QtBox2DTest::changeProperties(const QList<QWidget*>& widgets)
{
	QWidget* widget;

	QRect rt = ui.dockWidget->rect();
	if (mVerticalLayout)
	{
		delete mVerticalLayout;
	}
	mVerticalLayout = new QVBoxLayout(ui.dockWidgetContents);
	mVerticalLayoutWidget->setGeometry(ui.dockWidget->rect());
	mVerticalLayout->setContentsMargins(0, 0, 0, 0);
	foreach(widget, widgets)
	{
		mVerticalLayout->addWidget(widget); 
	}
	mVerticalLayoutWidget->setLayout(mVerticalLayout);
}

void QtBox2DTest::polygonTriggered(QAction* action)
{
	//mPolygonAction->setIcon(action->icon());
	ui.menuPolygon->menuAction()->disconnect(SIGNAL(triggered()));
	ui.menuPolygon->setIcon(action->icon());
	connect(ui.menuPolygon->menuAction(), SIGNAL(triggered()), action, SIGNAL(triggered()));
	ui.menuPolygon->setDefaultAction(action);
	//emit createTriggered(action);
}

QMenu* QtBox2DTest::getCreateMenu()
{
	static QMenu* createMenu = 0;
	if (createMenu == 0)
	{
		createMenu = new QMenu(this);
		createMenu->addActions(ui.menuCreate->actions());
		createMenu->addSeparator();
		createMenu->addAction(ui.actionPaste);
	}
	return createMenu;
}